<?php namespace Home\Controller;

use Hdphp\Controller\Controller;

//内容控制器
class ContentController extends Controller{
	protected $companyModel;
	protected $jobModel;
	public function __init(){
		$this->companyModel=new \Common\Model\Company;
		$this->jobModel=new \Common\Model\Job;
	}

	//职位内容
	public function index(){
		$jid=Q('get.jid',0,'intval');
		if ($jid) {
			//联表获得职位、公司、发布人的信息
			$data=$this->companyModel
				->join('job','lg_company_gsid','=','gsid')
				->join('user','lg_user_uid','=','uid')
				->join('job_data','lg_job_jid','=','jid')
				->where("jid=$jid")
				->find();
			//获得所属城市
			$cityModel=new \Common\Model\City;
			$pid=$cityModel->where("city_id={$data['lg_city_city_id']}")->pluck('city_pid');
			$city=$cityModel->where("city_id={$pid}")->pluck('city_name');
			$data['city']=$city;
			//简历处理率
			$statusModel=new \Common\Model\Status;
			$userJobModel=new \Common\Model\UserJob;
			$jobModel=new \Common\Model\Job;
			$jids=$jobModel->where("lg_company_gsid={$data['gsid']}")->lists('jid');
			$jids=implode(',',$jids);
			$allnum=$statusModel->where("lg_job_jid IN ({$jids})")->count();
			if (!$allnum) {
				$reg=0;
			}else{
				$delnum=$statusModel->where("status=0 AND lg_job_jid IN ({$jids})")->count();
				$reg=sprintf("%.2f",1-($delnum/$allnum));
			}
			$data['reg']=$reg*100;
			//判断该职位是否已经投递
			if (isset($_SESSION['homeInfo']['uid'])) {
				$deliver=$statusModel->where("lg_job_jid=$jid AND lg_user_uid={$_SESSION['homeInfo']['uid']}")->find();
				if ($deliver){
					$data['deliver']=1;
				}else{
					$data['deliver']=0;
				}
				//判断是否收藏了该职位
				$collect=$userJobModel->where("lg_job_jid=$jid AND lg_user_uid={$_SESSION['homeInfo']['uid']}")->find();
				if ($collect){
					$data['collect']=1;
				}else{
					$data['collect']=0;
				}
			}else{
				$data['deliver']=0;
				$data['collect']=0;
			}
			//相似职位
			$strr=substr($data['jname'],0,3);
			$sameData=Db::select("select * from lg_company as b join lg_job as a on a.lg_company_gsid=b.gsid where jid != {$data['jid']} and jname like '%{$strr}%' limit 4 ");
			foreach ($sameData as $k => $v) {
				$pid=$cityModel->where("city_id={$sameData[$k]['lg_city_city_id']}")->pluck('city_pid');
				$city=$cityModel->where("city_id={$pid}")->pluck('city_name');
				$sameData[$k]['city']=$city;
			}
			View::with('sameData',$sameData);
			View::with('data',$data);
		}else{
			View::error('请先选择您感兴趣的职位');
		}
		//最近浏览
		if (!isset($_SESSION['recent'])) {
			$_SESSION['recent'][]=[
				'jid'=>$jid,
				'jname'=>$data['jname'],
				'salary'=>$data['salary'],
				'gsname'=>$data['gsname']
			];
		}else{
			if ($_SESSION['recent'][0]['jid'] != $jid) {
				$_SESSION['recent'][1]=$_SESSION['recent'][0];
				$_SESSION['recent'][0]=[
					'jid'=>$jid,
					'jname'=>$data['jname'],
					'salary'=>$data['salary'],
					'gsname'=>$data['gsname']
				];
			}
		}
		View::make();
	}

	//公司内容
	public function company(){
		$gsid=Q('get.gsid',0,'intval');
		$data=$this->companyModel
					->join('company_data','gsid','=','lg_company_gsid')
					->where("gsid={$gsid}")
					->find();
		//获得上线职位的信息
		$jobData=$this->jobModel->where("lg_company_gsid={$data['gsid']}")->get();
		$num=count($jobData);
		View::with('num',$num);
		View::with('data',$data);
		View::make();
	}

	//公司职位
	public function jobMsg(){
		$gsid=Q('get.gsid',0,'intval');
		$data=$this->companyModel->where("gsid={$gsid}")->find();
		//获得上线职位的信息
		$jobData=$this->jobModel->where("lg_company_gsid={$data['gsid']}")->get();
		$num=count($jobData);
		View::with('num',$num);
		View::with('jobData',$jobData);
		View::with('data',$data);
		View::make();
	}





}